/**
 * Created by Brant on 2016/12/28.
 */
module.exports = function ($scope,customerBarService,caseBarDetailpeopleService,$cookies,$rootScope,caseBarDetailBarService) {
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate() + 1);
    //获取点击的案件的type
    $scope.type = $cookies.get('type');
    //获取案件的id
    $scope.caseId = $cookies.get('caseAppId');
    $scope.navcasebusinessname = $cookies.get('navcasebusinessname');
    $scope.navcasecausename = $cookies.get('navcasecausename');
    //当事人的东西
    $scope.dsrList = '';//当事人列表
    $scope.dsrname = '';
    $scope.dsrid = '';
    $scope.dsrneme = '';
    $scope.isUpdateDSR = 0;
    //添加的--自然人姓名、电话
    $scope.addDSRAppleInput = '';
    $scope.addNaturalPer.contactnumber = '';
    //添加--机构的姓名、法人姓名、电话
    $scope.addDSRComNameInput = '';
    $scope.addDSRComPerNameInput = '';
    $scope.addOrganiza.contactphone = '';

    $scope.isYG = 1;
    $scope.zusers = '';
    $scope.fusers = '';

    //对方当事人
    $scope.dfdsrList = '';
    $scope.addDFDSRInput = '';
    $scope.isDFYG = 3;
    $scope.isUpdateDFDSR = 0;

    //审判人
    $scope.sprList = '';
    $scope.addSPRInput = '';
    $scope.judgerole = '';


    //第三人
    $scope.dsanrList = '';
    $scope.addDSANRInput = '';
    $scope.job = '';
    $scope.role = '';

    //判断当事人是不是原告
    $scope.dsrIsYG = '';
    //判断对方当事人是不是原告
    $scope.dfdsrIsYG = '';


    //获取点击的案件的id
    var caseId = $cookies.get('caseAppId');
    /*登录ID*/
    $scope.loginId = kbt.decrypt($cookies.get('cookieUserId'));


    $scope.init = function () {
        /*如果是归档案件不可编辑*/
        if($scope.type==7){
            $('#btn-add-yg').hide();//当事人新增
            $('#btn-add-dfdsr').hide();//对方当事人新增
            $('#btn-add-sp').hide();//审判人新增
            $('#btn-add-third').hide();//第三人新增

            $('#btn-del-hide').attr("disabled","disabled");//删除
        }

        /*用于固定导航条*/
        sessionStorage.navNum = 2;
    };

    $scope.init();


    // //获取当事人列表
    $scope.getDSRList = function (caseId) {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarDetailpeopleService.getDSRList(caseId).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if(data.code == 1){
                $('.onecontentBody').css('display','block');
                $scope.dsrList = data.data;


                for(var i = 0;i < data.data.length ;i++){
                    $scope.dsrList[i].isShowDSR=true;
                    $scope.dsrid = $scope.dsrList.id;
                    $scope.isYG = $scope.dsrList[i].caseclienttype;
                    if($scope.dsrList[i].caseclienttype==1){
                        $scope.dsrIsYG = '(原告)';
                    }else if($scope.dsrList[i].caseclienttype==3){
                        $scope.dsrIsYG = '(被告)';
                    }
                }

            }else {

                $('.onecontentBody').css('display','none');
            }
        });
    };
    $scope.getDSRList(caseId);

    // //获取对方当事人列表1
    $scope.getDFDSRList = function (caseId) {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
      // for(var i = 0;i < $scope.dsrList.length ;i++){
      //   // $scope.dsrList[i].isShowDSR=true;
      //   // $scope.dsrid = $scope.dsrList.id;
      //   // $scope.isYG = $scope.dsrList[i].caseclienttype;
      //   if($scope.dsrList[0].caseclienttype==1){
      //     $scope.caseClientType = 3;
      //   }else if($scope.dsrList[0].caseclienttype==3){
      //     $scope.caseClientType = 1;
      //   }
      // }
        caseBarDetailpeopleService.getDFDSRList(caseId).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if(data.code == 1){
                $('.towcontentBody').css('display','block');
                $scope.dfdsrList = data.data;
                for(var i = 0;i < data.data.length ;i++){
                    $scope.dsrid = $scope.dsrList.id;
                    $scope.isDFYG = $scope.dfdsrList[i].caseclienttype;
                    if($scope.dfdsrList[i].caseclienttype == 1){
                        $scope.dfdsrIsYG = '(原告)';
                    }else if($scope.dsrList[i].caseclienttype==3){
                        $scope.dfdsrIsYG = '(被告)';
                    }
                }
            }else {

                $('.towcontentBody').css('display','none');
            }
        });
    };
    // $scope.getDFDSRList(caseId);


    // //获取审判人列表
    $scope.getSPRList = function (caseId) {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarDetailpeopleService.getSPRList(caseId).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if(data.code == 1){
                $('.threecontentBody').css('display','block');
                $scope.sprList = data.data;

                for (var i = 0; i < $scope.sprList.length;i++){
                    if($scope.sprList[i].judgerole == 1){
                        $scope.sprList[i].judgerole = '审判长';
                    }else if ($scope.sprList[i].judgerole == 2){
                        $scope.sprList[i].judgerole = '审判员';
                    }else if($scope.sprList[i].judgerole == 3){
                        $scope.sprList[i].judgerole = '书记员';
                    }else if($scope.sprList[i].judgerole == 4){
                        $scope.sprList[i].judgerole = '代理审判员'
                    }else {
                        $scope.sprList[i].judgerole = '人民陪审员';
                    }
                }

            }else {

                $('.threecontentBody').css('display','none');
            }
        });
    };
    // $scope.getSPRList(caseId);

    // //获取第三人列表
    $scope.getDSANRList = function (caseId) {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarDetailpeopleService.getDSANRList(caseId).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if(data.code == 1){
                $('.forecontentBody').css('display','block');
                $scope.dsanrList = data.data;

            }else {

                $('.forecontentBody').css('display','none');
            }
        });
    };
    // $scope.getDSANRList(caseId);

    /**
     * 取得全部参与人,当事人，对方当事人，审判人员，第三人
     * @param caseId
     */
    $scope.getTotalList = function(caseId){
        // 当事人，对方当事人，审判人员，第三人
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarDetailpeopleService.getTotalMemberLists(caseId).then(function (data) {
          $rootScope.loadingShow = false;
          $rootScope.fadeShow = false;
          if(data.code == 1) {
            // 当事人
            if (data.data.clients.length < 1)
              $('.onecontentBody').css('display', 'none');
            else
              $('.onecontentBody').css('display', 'block');
            $scope.dsrList = data.data.clients;
            for (var i = 0; i < $scope.dsrList.length; i++) {
              $scope.dsrList[i].isShowDSR = true;
              $scope.dsrid = $scope.dsrList.id;
              $scope.isYG = $scope.dsrList[i].caseclienttype;
              if ($scope.dsrList[i].caseclienttype == 1) {
                $scope.dsrIsYG = '(原告)';
              } else {
                $scope.dsrIsYG = '(被告)';
              }
            }

            // 对方当事人
            if (data.data.adclients.length < 1)
              $('.towcontentBody').css('display', 'none');
            else
              $('.towcontentBody').css('display', 'block');
            $scope.dfdsrList = data.data.adclients;
            for (var i = 0; i < $scope.dfdsrList.length; i++) {
              $scope.dsrid = $scope.dsrList.id;
              $scope.isDFYG = $scope.dfdsrList[i].caseclienttype;
              if ($scope.dfdsrList[i].caseclienttype == 1) {
                $scope.dfdsrIsYG = '(原告)';
              } else {
                $scope.dfdsrIsYG = '(被告)';
              }
            }
            // 审判人员
            if (data.data.judgements.length < 1)
              $('.threecontentBody').css('display', 'none');
            else
              $('.threecontentBody').css('display', 'block');
            $scope.sprList = data.data.judgements;

            for (var i = 0; i < $scope.sprList.length; i++) {
              if ($scope.sprList[i].judgerole == 1) {
                $scope.sprList[i].judgerole = '审判长';
              } else if ($scope.sprList[i].judgerole == 2) {
                $scope.sprList[i].judgerole = '审判员';
              } else if ($scope.sprList[i].judgerole == 3) {
                $scope.sprList[i].judgerole = '书记员';
              } else if ($scope.sprList[i].judgerole == 4) {
                $scope.sprList[i].judgerole = '代理审判员'
              } else {
                $scope.sprList[i].judgerole = '人民陪审员';
              }
            }
            // 第三人
            if (data.data.thirdParties.length < 1)
              $('.forecontentBody').css('display', 'none');
            else
              $('.forecontentBody').css('display', 'block');
            $scope.dsanrList = data.data.thirdParties;
          }
        });
    };

    $scope.getTotalList(caseId);

    //给审判人什么的加颜色
    $scope.setStyle = function(args) {  if(args == '审判长') return 'colorRed';  else if(args == '审判员') return 'colorGreen';  else if(args == '书记员') return 'colorOrange'; else if(args == '代理审判员') return 'colorBlue'; else if(args == '人民陪审员') return 'colorBlue1';};



  //添加当事人->个人
    $scope.addDSRAppleSPer = function (name,id) {

        var caseType = 1;
        if($scope.isYG == 1){
            caseType = 1;
        }else {
            caseType = 3;
        }
      for(var i = 0;i < $scope.dsrList.length ;i++){
        // $scope.dsrList[i].isShowDSR=true;
        // $scope.dsrid = $scope.dsrList.id;
        // $scope.isYG = $scope.dsrList[i].caseclienttype;
        if($scope.dsrList[0].caseclienttype==1){
          $scope.caseClientType = 1;
        }else if($scope.dsrList[0].caseclienttype==3){
          $scope.caseClientType = 3;
        }
      }
        //还得修改
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarDetailpeopleService.addDSR(caseId,caseType,$scope.caseClientType,name,id,$scope.loginId).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if (data.code == 1){
                //获取当事人列表
                $scope.getDSRList(caseId);
                $('.personpeplAddBox').css('display','none');
                $scope.addNaturalPer = {};
                $scope.searchPeoInput = '';
                $scope.zusers = '';
            }
        });
    };
    //添加当事人->个人
    $scope.addDSRAppleI = function () {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        if ($scope.addDSRAppleInput!= '' && $scope.addNaturalPer.contactnumber!= ''){
            var caseType = 1;
            if($scope.isYG == 1){
                caseType = 1;
            }else {
                caseType = 3;
            }
          for(var i = 0;i < $scope.dsrList.length ;i++){
            // $scope.dsrList[i].isShowDSR=true;
            // $scope.dsrid = $scope.dsrList.id;
            // $scope.isYG = $scope.dsrList[i].caseclienttype;
            if($scope.dsrList[0].caseclienttype==1){
              $scope.caseClientType = 1;
            }else if($scope.dsrList[0].caseclienttype==3){
              $scope.caseClientType = 3;
            }
          }
            caseBarDetailpeopleService.addDSR(caseId,caseType,$scope.caseClientType,$scope.addDSRAppleInput,$scope.loginId,$scope.addNaturalPer.contactnumber).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if (data.code == 1){
                    //获取当事人列表
                    $scope.getDSRList(caseId);
                    $('.personpeplAddBox').css('display','none');
                }
            });
            //添加到客户版
            caseBarDetailpeopleService.addZRRCustomer($scope.loginId,$scope.addDSRAppleInput,null,null,null,null,null,null,null,null,null).then(function (data) {
            });
        }
    };
    //添加当事人->公司
    $scope.addDSRComSPer = function (name,id) {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        var caseType = 1;
        if($scope.isYG == 1){
            caseType = 1;
        }else {
            caseType = 3;
        }
      for(var i = 0;i < $scope.dsrList.length ;i++){
        // $scope.dsrList[i].isShowDSR=true;
        // $scope.dsrid = $scope.dsrList.id;
        // $scope.isYG = $scope.dsrList[i].caseclienttype;
        if($scope.dsrList[0].caseclienttype==1){
          $scope.caseClientType = 1;
        }else if($scope.dsrList[0].caseclienttype==3){
          $scope.caseClientType = 3;
        }
      }
        //还得修改
        caseBarDetailpeopleService.addDSR(caseId,caseType,$scope.caseClientType,name,$scope.loginId,id).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if (data.code == 1){
                //获取当事人列表
                $scope.getDSRList(caseId);
                $('.personpeplAddBox').css('display','none');
                $scope.searchcusInput = '';
                $scope.addOrganiza = {};
                $scope.zusers = '';
                $scope.fusers = '';
            }
        });
    };
    //添加当事人->公司
    $scope.addDSRComI = function () {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        if ($scope.addDSRComNameInput != '' && $scope.addDSRComPerNameInput != '' && $scope.addOrganiza.contactphone != ''){
            var caseType = 1;
            if($scope.isYG == 1){
                caseType = 1;
            }else {
                caseType = 3;
            }
          for(var i = 0;i < $scope.dsrList.length ;i++){
            // $scope.dsrList[i].isShowDSR=true;
            // $scope.dsrid = $scope.dsrList.id;
            // $scope.isYG = $scope.dsrList[i].caseclienttype;
            if($scope.dsrList[0].caseclienttype==1){
              $scope.caseClientType = 1;
            }else if($scope.dsrList[0].caseclienttype==3){
              $scope.caseClientType = 3;
            }
          }
            caseBarDetailpeopleService.addDSR(caseId,caseType,$scope.caseClientType,$scope.addDSRComNameInput,$scope.addDSRComPerNameInput,$scope.addOrganiza.contactphone,$scope.loginId).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if (data.code == 1){
                    //获取当事人列表
                    $scope.getDSRList(caseId);
                    $('.personpeplAddBox').css('display','none');
                }
            });
            //添加到客户版
            caseBarDetailpeopleService.addFRCustomer($scope.loginId,$scope.addDSRComNameInput,null,null,null,null,null,null,null,null).then(function (data) {
            });
        }
    };

    //添加对方当事人
    $scope.addDFDSR = function () {

        if ($scope.addDFDSRInput != ''){
            var caseType = 1;
            if($scope.isDFYG == 1){
                caseType = 1;
            }else {
                caseType = 3;
            }

          for(var i = 0;i < $scope.dsrList.length ;i++){
            // $scope.dsrList[i].isShowDSR=true;
            // $scope.dsrid = $scope.dsrList.id;
            // $scope.isYG = $scope.dsrList[i].caseclienttype;
            if($scope.dsrList[0].caseclienttype==1){
              $scope.caseClientType = 3;
            }else if($scope.dsrList[0].caseclienttype==3){
              $scope.caseClientType = 1;
            }
          }

            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            caseBarDetailpeopleService.addDFDSR(caseId,caseType,$scope.caseClientType,$scope.addDFDSRInput,$scope.loginId).then(function(data){
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if(data.code == 1){
                    $scope.getDFDSRList(caseId);
                    var item = {
                      'id':data.data,
                      'adversaryclientname':$scope.addDFDSRInput
                    };
                    $scope.dfdsrList.push(item);
                    $('.footerAddBox2').css('display','none');
                    $scope.addDFDSRInput = '';
                }
            });
        }
    };

    //添加审判人
    $scope.addSPR = function () {

        if($scope.addSPRInput != '') {
            $scope.role = 1;
            $scope.job = $('.splist option:selected').html();

            if ($scope.job == '审判长') {
                $scope.role = 1;
            } else if ($scope.job == '审判员') {
                $scope.role = 2;
            } else if ($scope.job == '书记员') {
                $scope.role = 3;
            } else if ($scope.job == '代理审判员') {
                $scope.role = 4;
            } else {
                $scope.role = 5;
            }
            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            caseBarDetailpeopleService.addSPR(caseId, $scope.role, $scope.addSPRInput, $scope.loginId).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if (data.code == 1) {
                    // $scope.getSPRList(caseId);
                  var item = {
                    'id':data.data,
                    'judgename':$scope.addSPRInput,
                    'judgerole':$scope.job,
                  };
                  $scope.sprList.push(item);
                  $('.footerAddBox3').css('display', 'none');
                  $scope.addSPRInput = '';
                }
            });
        }
    };

    //添加第三人
    $scope.addDSANR = function () {

        if($scope.addDSANRInput != '') {
            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            caseBarDetailpeopleService.addDSANR(caseId, $scope.addDSANRInput, $scope.loginId).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if (data.code == 1) {
                    // $scope.getDSANRList(caseId);
                    var item = {
                      "id":data.data,
                      "thirdpartyname":$scope.addDSANRInput
                    };
                    $scope.dsanrList.push(item);
                    $('.footerAddBox4').css('display', 'none');
                    $scope.addDSANRInput = '';
                }
            });
        }
    };


    //删除当事人
    $scope.deleteDSR = function (id,caseClientName,index) {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        var len = $scope.dsrList.length;
        var caseName = "";
        if(len>1){
            caseName = $scope.dsrList[1].caseclientname+$scope.navcasecausename+$scope.navcasebusinessname;
            caseBarDetailpeopleService.deleteDSR(id,caseId,caseClientName,$scope.loginId,caseName).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if (data.code == 1){
                    $scope.getDSRList(caseId);
                    if (index == 0) {
                        caseBarDetailBarService.changeNavName(caseName);
                        $cookies.put('caseAppName', caseName, {
                            expires: expireDate
                        });
                    }
                }
            });
        }else{
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
          bootbox.dialog({
            message: "确定删除当事人",
            title: "提示信息",
            buttons: {
              Cancel: {
                label: "取消",
                className: "btn-cancel",
                style:'background:red',
                callback: function () {

                }
              },
              OK: {
                label: "确定",
                // className: "btn-default",
                className: "btn-confirm",
                // confirmButtonColor: "#DD6B55",
                callback: function () {

                }
              },
            }
          })
        }

    };

    //删除对方当事人
    $scope.deleteDFDSRPer = function (id,adversaryClientName) {
        bootbox.dialog({
        message: "确定删除当事人",
        title: "提示信息",
        buttons: {
          Cancel: {
            label: "取消",
            className: "btn-cancel",
            style:'background:red',
            callback: function () {

            }
          },
          OK: {
            label: "确定",
            className: "btn-confirm",
            callback: function () {
              caseBarDetailpeopleService.deleteDFDSR(id,caseId,adversaryClientName,$scope.loginId).then(function (data) {
                if (data.code == 1) {
                  $scope.getDFDSRList(caseId);
                }
              });
            }
          }
        }
      });
    };

    //删除审判人
    $scope.deleteSPR = function (id,judgeName) {
        bootbox.dialog({
        message: "确定删除审判人",
        title: "提示信息",
        buttons: {
          Cancel: {
            label: "取消",
            className: "btn-cancel",
            callback: function () {

            }
          },
          OK: {
            label: "确定",
            className: "btn-confirm",
            callback: function () {
              caseBarDetailpeopleService.deleteSPR(id,caseId,judgeName,$scope.loginId).then(function (data) {
                if (data.code == 1){
                  $scope.getSPRList(caseId);
                }
              });
            }
          }
        }
      });
    };

    //删除第三人
    $scope.deleteDSANR = function (id,thirdPartyName) {
      bootbox.dialog({
        message: "确定删除第三人",
        title: "提示信息",
        buttons: {
          Cancel: {
            label: "取消",
            className: "btn-cancel",
            callback: function () {

            }
          },
          OK: {
            label: "确定",
            className: "btn-confirm",
            callback: function () {
              caseBarDetailpeopleService.deleteDSANR(id,caseId,thirdPartyName,$scope.loginId).then(function (data) {
                if (data.code == 1){
                  $scope.getDSANRList(caseId);
                }
              });
            }
          }
        }
      });
    };












////修改当事人
//    $scope.updateDSR = function () {
//        if ($scope.isUpdateDSR == 1){
//            //修改完成
//            for(var i = 0;i < $scope.dsrList.length;i++){
//
//                caseBarDetailpeopleService.updateDSR($scope.dsrList[i].id,$scope.dsrList[i].caseclientname).then(function (data) {
//                    //修改完成
//                });
//
//            }
//
//        }
//    };
//
//    //修改对方当事人
//    $scope.updateDFDSR = function () {
//        if ($scope.isUpdateDFDSR == 1){
//            //修改完成
//            for(var i = 0;i < $scope.dfdsrList.length;i++){
//
//                caseBarDetailpeopleService.updateDFDSR($scope.dfdsrList[i].id,$scope.dfdsrList[i].adversaryclientname).then(function (data) {
//                    //修改完成
//                });
//
//            }
//
//        }
//    };

    //点击图片出现文本框,可以修改
    $('.oneImg').click(function () {
        if ($('.oneInput').css('display') == 'none'){
            $('.oneInput').css('display','block');
            $('.oneInput').siblings('.name').css('display','none');
            $scope.isUpdateDSR = 0;
        }else {
            $('.oneInput').css('display','none');
            $('.oneInput').siblings('.name').css('display','block');
            $scope.isUpdateDSR = 1;
        }

    });
    $('.towImg').click(function () {
        if ($('.towInput').css('display') == 'none'){
            $('.towInput').css('display','block');
            $('.towInput').siblings('.name').css('display','none');
            $scope.isUpdateDFDSR = 0;
        }else {
            $('.towInput').css('display','none');
            $('.towInput').siblings('.name').css('display','block');
            $scope.isUpdateDFDSR = 1;
        }

    });
    $('.threeImg').click(function () {
        if ($('.threeInput').css('display') == 'none'){
            $('.threeInput').css('display','block');
            $('.threeInput').siblings('span').css('display','none');
        }else {
            $('.threeInput').css('display','none');
            $('.threeInput').siblings('span').css('display','block');
        }

    });
    $('.foreImg').click(function () {
        if ($('.foreInput').css('display') == 'none'){
            $('.foreInput').css('display','block');
            $('.foreInput').siblings('.name').css('display','none');
        }else {
            $('.foreInput').css('display','none');
            $('.foreInput').siblings('.name').css('display','block');
        }

    });



    $('.footerAdd').click(function(){
        $(this).siblings('.footerAddBox').toggle();
        $(this).siblings('.personpeplAddBox').toggle();
        $('.peoeplAddDivliIn').css('display','block');
        $('.cuseplAddDivliIn').css('display','block');
        $scope.phoneEmpty = false;
        $scope.phoneValid = false;
    });
    $('.dsrcannel').click(function () {
        $(this).parents('.footerAddBox').css('display','none');
        // $scope.addDFDSRInput = '';
        // $scope.addSPRInput = '';
        // $scope.addDSANRInput = '';
    });


    //当事人点击效果
    /*=======个人和公司切换=====*/
    $(".addpeople").on("click",function(){
        var i = $(this).index();
        $(this).addClass("active").siblings().removeClass("active");
        $(this).find("img").attr("src","img/pepleactiveicon.png");
        $(this).siblings().find("img").attr("src","img/companyicon.png");
        $(this).parent(".eplAddUl").siblings(".eplAddbody ").find(".eplAddDivli").addClass("hide");
        $(this).parent(".eplAddUl").siblings(".eplAddbody").find(".eplAddDivli").eq(i).removeClass("hide");
    });
    $(".addcompany").on("click",function(){
        var i = $(this).index();
        $(this).addClass("active").siblings().removeClass("active");
        $(this).find("img").attr("src","img/companyactiveicon.png");
        $(this).siblings().find("img").attr("src","img/pepleicon.png");
        $(this).parent(".eplAddUl").siblings(".eplAddbody ").find(".eplAddDivli").addClass("hide");
        $(this).parent(".eplAddUl").siblings(".eplAddbody").find(".eplAddDivli").eq(i).removeClass("hide");
    });

    /*======直接选择和添加切换=====*/
    $(".eplAddDivliUl li").on("click",function(){
        var i = $(this).index();
        $(this).addClass("active").siblings().removeClass("active");
        $(this).parent(".eplAddDivliUl").siblings(".eplAddDivDiv").find(".eplAddDivliIn").addClass("hide");
        $(this).parent(".eplAddDivliUl").siblings(".eplAddDivDiv").find(".eplAddDivliIn").eq(i).removeClass("hide");
    });

    /*=====添加律师成员=======*/
    $(".LawyerList li").on("click",function(){
        var i = $(this).index();
        $(this).addClass("active").siblings().removeClass("active");
        $(this).parent().siblings(".LawyerDiv").find(".Lawyerbox").addClass("hide");
        $(this).parent().siblings(".LawyerDiv").find(".Lawyerbox").eq(i).removeClass("hide");
    });
    $(".addlvshi").on("click",function(){
        $(".LawyerAddBody").removeClass("hide");
    });
    $(".LawyerAddBody .Add-close").on("click",function(){
        $(".LawyerAddBody").addClass("hide");
    })
    //点击取消
    $('.eplAddDivBtn a').click(function () {
        $(this).parents('.personpeplAddBox').css('display','none');
    });


    //直接添加个人搜索框
    $scope.searchPeoInput = '';

    $('.peoeplAddDivliIn').css('display','none');


    //个人
    $scope.searchPeople2KHBZ = function(){
                $rootScope.loadingShow = true;
                $rootScope.fadeShow = true;
                caseBarDetailpeopleService.selectZiRanRenInfo($scope.loginId, null, null, $scope.searchPeoInput,$scope.caseId).then(function (data) {
                    $rootScope.loadingShow = false;
                    $rootScope.fadeShow = false;
                    if (data.code == 1) {
                        $scope.zusers = data.data;
                    }
                }, function () {

                });

    };


    //直接添加公司搜索框
    $scope.searchcusInput = '';

    //公司
    $scope.searchPeople2KHBF = function () {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarDetailpeopleService.selectFaRenInfo($scope.loginId, null, null, $scope.searchcusInput,$scope.caseId).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if (data.code == 1) {
                $scope.fusers = data.data;
            }
        }, function () {

        });
    };


    /**
     * 编辑当事人名字
     * @param name 相关人员名称
     * @param status 状态：1-当事人，2-对方当事人，3-审判人员，4-第三人
     */
    $scope.editDSPeople = function (name, $event, status) {
        if($scope.type != 7){
            var $event = $event.target;
            var e = $($event);
            if (status == 1) {//当事人
                $(e).siblings('.DSPeople').css('display', 'block');
                $(e).css('display', 'none');
            } else if (status == 2) {//对方当事人
                $(e).siblings('.DFSPeople').css('display', 'block');
                $(e).css('display', 'none');
            } else if (status == 3) {//审判人员
                $(e).siblings('.SPRPeople').css('display', 'block');
                $(e).css('display', 'none');
            } else if (status == 4) {//第三人
                $(e).siblings('.DSRPeople').css('display', 'block');
                $(e).css('display', 'none');
            }
        }
        // $scope.dsrList[index].isShowDSR = false;
    };

    /**
     * 回车修改相关人员名字
     * @param $event 当前对象
     * @param caseid 案件ID
     * @param id 相关人员ID
     * @param caseclientname 相关人员名称
     * @param status 状态：1-当事人，2-对方当事人，3-审判人员，4-第三人
     */
    $scope.keyDSPeople = function ($event, caseid, id, caseclientname,status,index) {
        var $event = $event.target;
        var e = $($event);
        $(document).keyup(function (event) {
            if (event.keyCode == '13') {
                var mName=$('#ds'+id).val();
                $rootScope.loadingShow = true;
                $rootScope.fadeShow = true;
                var caseName = "";
                if(status==1){
                    caseName = mName+$scope.navcasecausename+$scope.navcasebusinessname;
                }else{
                    caseName = "";
                }
                caseBarDetailpeopleService.updateCaseRelatedPersonName($scope.loginId, caseid, status, mName, id,caseName).then(function (data) {
                    $rootScope.loadingShow = false;
                    $rootScope.fadeShow = false;
                    if (data.code == 1) {
                        if (status == 1) {//当事人
                            $(e).css('display', 'none');
                            $(e).siblings('#DSname').css('display', 'block');
                            if(index==0){
                                caseBarDetailBarService.changeNavName(caseName);
                                $cookies.put('caseAppName',caseName,{
                                    expires: expireDate
                                });
                            }
                        } else if (status == 2) {//对方当事人
                            $(e).css('display', 'none');
                            $(e).siblings('#DFname').css('display', 'block');
                        } else if (status == 3) {//审判人员
                            $(e).css('display', 'none');
                            $(e).siblings('#SPRname').css('display', 'block');
                        } else if (status == 4) {//第三人
                            $(e).css('display', 'none');
                            $(e).siblings('#DSRname').css('display', 'block');
                        }
                    }
                }, function (response) {
                    $rootScope.loadingShow = false;
                    $rootScope.fadeShow = false;
                    console.error('updateCaseRelatedPersonName error', response.status, response.statusText);
                });
            }
        });
    };

  $scope.selectConsigner = selectConsigner;

  $scope.delPer = delPer;
  function delPer(index) {
    $scope.dsrList.splice(index,1);
  };


  //案件所属
  function caseBelongs() {
    customerBarService.caseBelongs().then(function (response) {
      $scope.teamList = response.data.data;
    })
  }

  //从列表选择委托人
  function selectConsigner(natrualPer, type) {
    if (natrualPer == null) {
      return;
    }
    var flag = false;
    for(var i=0;i<$scope.dsrList.length; i++){
      if ($scope.dsrList[i].id == natrualPer.customerpersonId) {
        flag = true;
      }
    }
    var item = {
      id: natrualPer.customerpersonId,
      caseclientname: natrualPer.name,
      caseclienttype : type,
      dsrtextmm: true
    };
    if(!flag){
      $scope.dsrList.push(item);
    }
    $scope.showModule.addModule = false;
    $scope.showModule.searchList = false;
    $scope.searchName.name = '';
  }

  //法人手机号码验证
  $scope.phoneEmpty = false;
  $scope.phoneValid = false;
  $scope.checkMobileNaturalPer = checkMobileNaturalPer;
  $scope.checkMobileOrganiza = checkMobileOrganiza;
  function checkMobileNaturalPer(){
    var phone = $scope.addNaturalPer.contactnumber;
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if(phone == ''){
      $scope.phoneEmpty = true;
    }else if(phone.length !=11){
      $scope.phoneValid = true;
    }else if(!myreg.test(phone)){
      $scope.phoneValid = true;
    }else{
      $scope.phoneEmpty = false;
      $scope.phoneValid = false;
    }
    return false;
  }

  //机构手机号码验证
  function checkMobileOrganiza(){
    var phone = $scope.addOrganiza.contactphone;
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if(phone == ''){
      $scope.phoneEmpty = true;
    }else if(phone.length !=11){
      $scope.phoneValid = true;
    }else if(!myreg.test(phone)){
      $scope.phoneValid = true;
    }else{
      $scope.phoneEmpty = false;
      $scope.phoneValid = false;
    }
    return false;
  }
  $scope.addNaturalPer.name = '';
  $scope.addNaturalPer.contactnumber = '';
  $scope.addOrganiza.companyname = '';
  $scope.addOrganiza.contactname = '';
  $scope.addOrganiza.contactphone = '';
  $scope.savePersonnelPer = savePersonnelPer;
  $scope.savePersonnelOrganiza = savePersonnelOrganiza;
  //保存自然人
  function savePersonnelPer(addNaturalPer) {
    if($scope.phoneEmpty){
      return;
    }else if($scope.phoneValid){
      return;
    }
    customerBarService.addZRRCustomer(addNaturalPer).then(function (data) {
      if(data.code == 1){
        var item = {
          "caseclientname":data.data.name,
          id:data.data.id,
          type:1
        };
        $scope.dsrList.push(item);
        $('.personpeplAddBox').css('display','none');
        $scope.addDSRAppleSPer(data.data.name,data.data.id);
      }
    });
  }

  //保存机构
  function savePersonnelOrganiza(addOrganiza) {
    if($scope.phoneEmpty){
      return;
    }else if($scope.phoneValid){
      return;
    }
    customerBarService.addFRCustomer(addOrganiza).then(function (data) {
      if(data.code==1){
        var item = {
          id:data.data.id,
          type:2,
          organizaName:data.data.companyname,
          name:data.data.contactname,
          phone:data.data.contactphone,
        };
        $scope.dsrList.push(item);
        $scope.addDSRComSPer(data.data.companyname,data.data.id);
        $('.personpeplAddBox').css('display','none');
      }
    })
  };

  //获取div中点击事件
  $('.footerAdd').mousedown(function(e){//&&$(e.target)!=$('list')
    //使用e.target 判断事件源 是否在 dropDown或者list上 来处理是否隐藏
    if($(e.target)!=$('.footerAdd')){
      $('.personpeplAddBox').hide();
      $('.footerAddBox').hide();
    }
  });


};
